package sights.scenic_spot.servlet;

import com.alibaba.fastjson.JSONObject;
import netscape.javascript.JSObject;
import sights.scenic_spot.model.Scenic_spot;
import sights.scenic_spot.service.SpotService;
import sights.scenic_spot.service.SpotServiceImpl;
import util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(value = "*.spot")
public class SpotServlet extends HttpServlet {
    // 初始化服务层对象
    private SpotService spotService = new SpotServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        switch (servletPath) {
            case"/toList.spot":
                toList(req,resp);
                break;
            case "/selectByPage.spot":
                selectByPage(req, resp);
                break;
            case "/selectByHot.spot":
                selectByHot(req, resp);
                break;
            case"/toEdit.spot":
                toEdit(req,resp);
                break;
            case"/queryById.spot":
                queryById(req,resp);
                break;
            case"/checkName.spot":
                checkName(req,resp);
                break;
            case"/saveOne.spot":
                saveOne(req,resp);
                break;
            case"/DeleteBySightId.spot":
                DeleteBySightId(req,resp);
                break;
            default:
                break;
        }
    }
    private void toList(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getRequestDispatcher("/spot_list.html").forward(req, resp);
    }
    /**
     * 分页查询接口
     */
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            PrintWriter writer = resp.getWriter();
            String pageNoStr = req.getParameter("pageNo");
            String likeValue = req.getParameter("likeValue") == null ? "" : req.getParameter("likeValue");
            int pageNo = pageNoStr == null || pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
            PageModel pageModel = new PageModel();
            pageModel.setPageNo(pageNo);
            pageModel.setLikeValue(likeValue);
            List<Scenic_spot> spots = spotService.selectByPage(pageModel);
            pageModel.setRecordList(spots);
            String jsonStr = JSONObject.toJSONString(pageModel);
            writer.print(jsonStr);

    }

    /**
     * 热门景点查询接口
     */
    private void selectByHot(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            PrintWriter writer = resp.getWriter();
            String pageNoStr = req.getParameter("pageNo");
            String likeValue = req.getParameter("likeValue") == null ? "" : req.getParameter("likeValue");
            String hotStr = req.getParameter("is_hot");
            int pageNo = pageNoStr == null || pageNoStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
            int isHot = hotStr == null || hotStr.isEmpty() ? 0 : Integer.parseInt(hotStr);
            PageModel pageModel = new PageModel();
            pageModel.setPageNo(pageNo);
            pageModel.setLikeValue(likeValue);
            List<Scenic_spot> spots = spotService.selectByHot(pageModel, isHot);
            pageModel.setRecordList(spots);
            String jsonStr = JSONObject.toJSONString(pageModel);
            writer.print(jsonStr);
    }
    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().setAttribute("sightToken","sightTokenValue");
        String sight_id=req.getParameter("sight_id");
        resp.sendRedirect(req.getContextPath()+"/spot_edit.html?sight_id="+sight_id);
    }
    private void queryById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer=resp.getWriter();
        String id=req.getParameter("sight_id");
        Integer sight_id=Integer.valueOf(id);
        Scenic_spot scenic_spot= spotService.selectById(sight_id);
        String jsonStr=JSONObject.toJSONString(scenic_spot);
        writer.print(jsonStr);
    }
    private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer=resp.getWriter();
        String sight_name=req.getParameter("sight_name");
        Long flag=spotService.findBySightName(sight_name);
        Map<String, Object> result = new HashMap<>();
        result.put("flag", flag);
        String jsonStr=JSONObject.toJSONString(result);
        writer.print(jsonStr);

    }
    private void saveOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //防止重复提交
        Object o=req.getSession().getAttribute("sightToken");
        if (o!=null){
            req.getSession().removeAttribute("sightToken");
        PrintWriter writer=resp.getWriter();
        String sight_id=req.getParameter("sight_id");
        //修改
        if (sight_id!=null&&!"".equals(sight_id)){
            String sight_name=req.getParameter("sight_name");
            String city=req.getParameter("city");
            String introduction=req.getParameter("introduction");
            String img_path=req.getParameter("img_path");
            String isHot = req.getParameter("is_hot");
            Integer isHotInt = "on".equals(isHot) ? 1 : 0;
            Scenic_spot scenic_spot=new Scenic_spot(Integer.valueOf(sight_id),sight_name,city,introduction,img_path,isHotInt);
            spotService.updateOne(scenic_spot);
        }else {
            String sight_name = req.getParameter("sight_name");
            String city = req.getParameter("city");
            String introduction = req.getParameter("introduction");
            String img_path = req.getParameter("img_path");
            String isHot = req.getParameter("is_hot");
            Integer isHotInt = "on".equals(isHot) ? 1 : 0;
            Scenic_spot scenic_spot=new Scenic_spot(null,sight_name,city,introduction,img_path,isHotInt);
            spotService.saveOne(scenic_spot);
        }
            resp.sendRedirect(req.getContextPath()+"/toList.spot");
    }else
            resp.sendRedirect(req.getContextPath()+"/token.html");
}
    private void DeleteBySightId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sight_id=req.getParameter("sight_id");
        Scenic_spot scenic_spot=new Scenic_spot(Integer.valueOf(sight_id),null,null,null,null,null);
        spotService.deleteOne(scenic_spot);
        resp.sendRedirect(req.getContextPath()+"/toList.spot?pageNo=1");
    }
}
